<?php

namespace App\Model;

use Illuminate\Support\Carbon;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Model\SaAdminLoginLog.
 *
 * @property int         $id
 * @property int         $admin_id   管理员id
 * @property string      $login_ip   登录ip
 * @property Carbon|null $created_at 创建时间
 * @property Carbon|null $updated_at 更新时间
 * @property Carbon|null $deleted_at 删除时间
 *
 * @method static \Illuminate\Database\Eloquent\Builder|SaAdminLoginLog newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|SaAdminLoginLog newQuery()
 * @method static Builder|SaAdminLoginLog onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|SaAdminLoginLog query()
 * @method static \Illuminate\Database\Eloquent\Builder|SaAdminLoginLog whereAdminId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SaAdminLoginLog whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SaAdminLoginLog whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SaAdminLoginLog whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SaAdminLoginLog whereLoginIp($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SaAdminLoginLog whereUpdatedAt($value)
 * @method static Builder|SaAdminLoginLog withTrashed()
 * @method static Builder|SaAdminLoginLog withoutTrashed()
 * @mixin \Eloquent
 */
class SaAdminLoginLog extends BaseModel
{
    use SoftDeletes;
    protected $table   = 'sa_admin_login_log';

    /**
     * 保存登录信息.
     *
     * @param array $data
     *
     * @return mixed
     */
    public static function saveInfo($data = [])
    {
        $info = [
            'admin_id'   => $data['id'],
            'login_ip'   => $data['ip'],
        ];

        return self::insertGetId($info);
    }

    /**
     * 获取登录日志.
     *
     * @param $where
     * @param $limit
     *
     * @return mixed
     */
    public static function getLoginLog($where, $limit)
    {
        $data = self::from('sa_admin_login_log as all')
                ->where($where)
                ->select('all.id', 'a.username', 'r.name as role_name', 'all.login_ip', 'all.created_at')
                ->leftjoin('sa_admin as a', 'a.id', '=', 'all.admin_id')
                ->leftjoin('sa_role as r', 'r.id', '=', 'a.role_id')
                ->orderBy('all.id', 'desc')
                ->paginate($limit)
                ->toArray();

        return $data;
    }
}
